package com.smbms.controller;

import java.io.File;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.smbms.entity.SmbmsProvider;
import com.smbms.service.ProviderService;

/**
 * 供应商控制器
 * @ClassName: ProviderContoller 
 * @Description: (这里用一句话描述这个类的作用) 
 * @author Lyrk.Guo
 * @date 2018年7月12日 下午2:25:49
 */
@Controller
@RequestMapping("/provider")
public class ProviderContoller {
	
	@Autowired
	ProviderService providerService;

	@RequiresPermissions("provider_list")
	@RequestMapping("/list")
	public String list(Model model){
		List<SmbmsProvider> providerList = providerService.selectProviders();
		model.addAttribute("providerList", providerList);
		return "providerlist";
	}
	
	@RequestMapping("/add")
	public String add(SmbmsProvider provider,
			Model model,
			MultipartFile companyLicPic){
		//保存文件
		if(!companyLicPic.isEmpty()){
			try {
				companyLicPic.transferTo(new File("E:\\smbmsFiles\\"+companyLicPic.getOriginalFilename()));
				//设置保存后的文件名
				provider.setCompanyLicPicPath(companyLicPic.getOriginalFilename());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		int n = providerService.add(provider);
		if(n>0){
			return "redirect:list";
		}
		model.addAttribute("msg", "添加失败");
		return "provideradd";
	}
}
